package com.wunong.smart.commission.domain.state.operation.impl;

import com.wunong.smart.commission.application.context.CommissionContext;
import com.wunong.smart.commission.application.request.support.CreateCommissionRequest;
import com.wunong.smart.commission.client.enums.CommissionOperationEnum;
import com.wunong.smart.commission.domain.business.CancelCommissionBusinessExt;
import com.wunong.smart.commission.domain.model.CommissionDomain;
import com.wunong.smart.commission.domain.state.operation.AbstractCommissionOperation;
import com.wunong.smart.domain.platform.common.VerifyDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author create by zealot.zt
 */
@Slf4j
@Component
public class CancelCommissionOperation extends AbstractCommissionOperation<CreateCommissionRequest> {

    @Resource
    private CancelCommissionBusinessExt cancelCommissionBusinessExt;

    @Override
    public VerifyDTO canOperate(CommissionContext<CreateCommissionRequest> context) {
        VerifyDTO canCancel = cancelCommissionBusinessExt.canCancel(context.getRequest(), context.getModel());
        if (canCancel.isNonAccess()) {
            return canCancel;
        }

        return VerifyDTO.available();
    }

    @Override
    protected Object doOperate(CommissionContext<CreateCommissionRequest> context) {
        CommissionDomain domain = context.getModel();

        domain.cancel();
        commissionRepository.update(domain);

        log.warn("commissionOperation#cancel, success, user:{}, source:{}, id:{}",
                domain.getUser(), domain.getSource(), domain.getId());

        return domain;
    }

    @Override
    public CommissionOperationEnum getOperationEnum() {
        return CommissionOperationEnum.CANCEL;
    }

}
